package joyi.train.employee.service.impl;

import j.data.tree.TreeData;
import j.data.tree.TreeNode;
import j.data.tree.TreeSetting;
import j.framework.core.ServiceContext;
import j.util.New;
import j.util.StringUtils;
import j.util.jdbc.JdbcTemplate;
import j.util.jdbc.RowMapper;
import j.util.pkgenerator.PG;
import joyi.train.employee.domain.Department;
import joyi.train.employee.service.DepartmentService;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by WY2015092804 on 2015/10/20.
 */
public class DepartmentServiceImpl implements DepartmentService{
    @Override
    public Map<String, Object> getDepartmentTree(ServiceContext serviceContext) {
        List<Map<String, Object>> list = serviceContext.getJdbcTemplate().queryForListMap("select departmentId id , departmentName name , parentDepartmentId _parentId from cw_Department");
        Map<String, Object> map = New.map();
        map.put("rows", list);
        map.put("total", list.size());
        return map;
    }

    @Override
    public Department getDepartment(ServiceContext serviceContext) {
        String id = serviceContext.params.getString("id");
        if (StringUtils.isNotEmpty(id)) {
            Map<String, Object> map = New.map();
            map.put("departmentId", id);
            Department department = serviceContext.getJdbcTemplate().findByPk(Department.class, map);
            return department;
        }
        String rowId = serviceContext.params.getString("rowId");
        Department department = new Department();
        if (StringUtils.isNullOrEmpty(rowId)){
            //rowId = "DEPT2015102100000001";
            //如果没有选中行 默认为部门的根目录
            Map<String,Object> map= serviceContext.getJdbcTemplate().queryForMap("select departmentId from cw_Department where parentDepartmentId is null");
            department.setParentDepartmentId(map.get("departmentId").toString());
        }else {
            department.setParentDepartmentId(rowId);
        }
        return department;
    }

    @Override
    public void saveOrUpdateDepartment(ServiceContext serviceContext) {
        Department department = serviceContext.getBean(Department.class);
        department.setNote(department.getNote().trim());

        if (StringUtils.isNullOrEmpty(department.getDepartmentId())) {
            department.setDepartmentId(PG.getNext("DEP"));
            department.setAddedUserId(serviceContext.getUser().getUserId());
            department.setAddedTime(new Date());
            serviceContext.getJdbcTemplate().insert(department);
            return;
        }
        Map<String, Object> map = New.map();
        map.put("departmentId", department.getDepartmentId());
        Department department1 = serviceContext.getJdbcTemplate().findByPk(Department.class, map);
        if (department.equals(department1)) {
            serviceContext.actionResult.add("err", "上属部门不能是部门本身");
        } else {
            serviceContext.getJdbcTemplate().update(department);
        }
    }

    @Override
    public TreeData getDepartmentCombotree(ServiceContext serviceContext) {
        List<Department> list = serviceContext.getJdbcTemplate().queryForList("select * from cw_Department", new RowMapper<Department>() {
            @Override
            public Department mapRow(ResultSet rs, int rowNum) throws SQLException {
                Department department = new Department();
                department.setDepartmentId(rs.getString("departmentId"));
                department.setDepartmentName(rs.getString("departmentName"));
                department.setNote(rs.getString("note"));
                department.setParentDepartmentId(rs.getString("parentDepartmentId"));
                return department;
            }
        });
        TreeSetting setting = new TreeSetting(false, false);
        setting.setNameFieldName("text");
        TreeData treeData = new TreeData(setting);
        for (Department department : list) {
            TreeNode treeNode = new TreeNode(department.getDepartmentId(), department.getDepartmentName());
            treeNode.setpId(department.getParentDepartmentId());
            treeData.addNode(treeNode);
        }
        return treeData;
    }

    @Override
    public String delDepartment(ServiceContext serviceContext) {
        String departmentId = serviceContext.params.getString("departmentId");
        if (StringUtils.isNotEmpty(departmentId)) {
            JdbcTemplate jdbcTemplate = serviceContext.getJdbcTemplate();

            if (jdbcTemplate.queryForInt("select count(1) from cw_Department where parentDepartmentId=? ",departmentId) > 0) {
                serviceContext.actionResult.add("err", "该部门有下属部门");
                return null;
            } else if (jdbcTemplate.queryForInt("select count(1) from cw_Employee e where e.departmentId = ?"
                    , departmentId) > 0) {
                serviceContext.actionResult.add("err","该部门有员工");
                return null;
            } else if (departmentId.equals(jdbcTemplate.queryForMap("select departmentId from cw_Department where parentDepartmentId is null").get("departmentId"))){
                serviceContext.actionResult.add("err","部门跟不能删除");
                return null;
            }
            Department department = new Department();
            department.setDepartmentId(departmentId);
            jdbcTemplate.delete(department);
            return "删除成功";
        }
        serviceContext.actionResult.add("err", "你没有指定要删除部门");
        return null;
    }
}
